package com.leonwoodstudio.endorsementclient;

import android.content.Context;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Properties;

/**
 * Created by win7 on 2017/3/16.
 */
public class Util {
    public static final String ACTION_FILE_RECEIVED = "com.leonwoodstudio.service.ACTION_FILE_RECEIVED";
    public static final String PROPERTIES_KEY_DEFAULT_USER  = "default_user";
    public static final String PROPERTIES_KEY_ALL_USERS  = "all_users";
    public static final String PROPERTIES_KEY_LOCAL_PORT = "local_port";
    public static final String PROPERTIES_KEY_REMOTE_PORT = "remote_port";
    public static final String PROPERTIES_KEY_REMOTE_IP_ADDRESS = "remote_ip_address";

    public static byte[] int2ByteArray(int src) {
        byte[] res = new byte[4];
        res[3] = (byte) (src & 0xff);// 最低位
        res[2] = (byte) ((src >> 8) & 0xff);// 次低位
        res[1] = (byte) ((src >> 16) & 0xff);// 次高位
        res[0] = (byte) (src >>> 24);// 最高位,无符号右移。
        return res;
    }

    public static int byteArray2Int(byte[] bytes) {
        int res;
        res = bytes[0] & 0xFF;
        res = (res << 8) | (bytes[1] & 0xff) ;
        res = (res << 8) | (bytes[2] & 0xff) ;
        res = (res << 8) | (bytes[3] & 0xff) ;
        return res;
    }

    public static byte[] commandHead() {
        byte[] targets = new byte[] { 0x00, 0x00, 0x00, 0x02};
        return targets;
    }

    public static byte[] commandTail() {
        byte[] targets = new byte[] { 0x00, 0x00, 0x00, 0x7F};
        return targets;
    }

    public static byte[] connectByteArray(byte[] data1, byte[] data2) {
        byte[] data3 = new byte[data1.length + data2.length];
        System.arraycopy(data1, 0, data3, 0, data1.length);
        System.arraycopy(data2, 0, data3, data1.length, data2.length);
        return data3;
    }

    public static Properties getProperties(Context context){
        Properties props = new Properties();
        try {
            File saved_config = new File(FileAccessor.getInstance(null).getFilesDir() + "config.properties");
            InputStream is;
            if (saved_config.exists()) {
                is = new FileInputStream(saved_config);
            } else {
                //方法一：通过activity中的context攻取setting.properties的FileInputStream
                is = context.getAssets().open("config.properties");
                //方法二：通过class获取setting.properties的FileInputStream
                //InputStream is = Util.class.getResourceAsStream("/assets/config.properties");
            }
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            props.load(br);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return props;
    }

    public static void saveProperties(Properties props) {
        File to_save_config = new File(FileAccessor.getInstance(null).getFilesDir() + "config.properties");
        OutputStream os;
        BufferedWriter bw = null;
        try {
            os = new FileOutputStream(to_save_config);
            bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            props.store(bw, null);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static String getHostIP() {
        String hostIp = null;
        try {
            Enumeration nis = NetworkInterface.getNetworkInterfaces();
            InetAddress ia = null;
            while (nis.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) nis.nextElement();
                Enumeration<InetAddress> ias = ni.getInetAddresses();
                while (ias.hasMoreElements()) {
                    ia = ias.nextElement();
                    if (ia instanceof Inet6Address) {
                        continue;// skip ipv6
                    }
                    String ip = ia.getHostAddress();
                    if (!"127.0.0.1".equals(ip)) {
                        hostIp = ia.getHostAddress();
                        break;
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return hostIp;
    }
}
